﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;


public partial class Admin1_Default2 : System.Web.UI.Page
{
    Datacon dataconn = new Datacon();
    protected void Page_Load(object sender, EventArgs e)
    {
        ImageButton4.Attributes.Add("onclick", "javascript:return window.confirm('您确定要删除选中项吗?')");
        if (!IsPostBack)
        {
            //dataconn.bindinfostring(GridView1, "select * from Student order by ID DESC", "ID");

        }

    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        if (TextBox1.Text == "")
        {
            dataconn.bindinfostring(GridView1, "SELECT * FROM danwei order by danweimingchen", "ID");
        }
        else
        {
            dataconn.bind(GridView1, "SELECT * FROM danwei  where danweimingchen like '%" + TextBox1.Text + "%' order by danweimingchen");
        }

    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        dataconn.eccom("delete from Student where ID='" + GridView1.DataKeys[e.RowIndex].Value + "'");
        if (TextBox1.Text == "")
        {
            dataconn.bindinfostring(GridView1, "SELECT a.[ID] as id,a.bumenid,a.danweiid, a.[Name], a.[kaoshenghao], b.danweimingchen, c.bumenmingcheng FROM [Student] a,danwei b,bumeng c where a.danweiid=b.id and a.bumenid=c.id order by a.[Name]", "ID");
        }
        else
        {
            dataconn.bind(GridView1, "SELECT a.[ID] as id,a.bumenid,a.danweiid, a.[Name], a.[kaoshenghao], b.danweimingchen, c.bumenmingcheng FROM [Student] a,danwei b,bumeng c where a.danweiid=b.id and a.bumenid=c.id and a.name='" + TextBox1.Text + "' order by a.[Name]");
        }

        //Response.Redirect("ManageStudent.aspx");
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        if (TextBox1.Text == "")
        {
            dataconn.bindinfostring(GridView1, "SELECT a.[ID],a.bumenid,a.danweiid, a.[Name], a.[kaoshenghao], b.danweimingchen, c.bumenmingcheng FROM [Student] a,danwei b,bumeng c where a.danweiid=b.id and a.bumenid=c.id order by a.[Name]", "ID");
        }
        else
        {
            dataconn.bind(GridView1, "SELECT a.[ID],a.bumenid,a.danweiid, a.[Name], a.[kaoshenghao], b.danweimingchen, c.bumenmingcheng FROM [Student] a,danwei b,bumeng c where a.danweiid=b.id and a.bumenid=c.id and a.name='" + TextBox1.Text + "' order by a.[Name]");
        }
    }
  
    protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
    {
        
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        
    }
    protected void ImageButton4_Click(object sender, ImageClickEventArgs e)
    {
       
        Response.Redirect("ManageStudent.aspx");
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        dataconn.bind(GridView1, "select * from Student  order by ID DESC");
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        SqlConnection cn = dataconn.getcon();
        cn.Open();
        string ID = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString();
        string Name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString();
        string PWD = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString(); 
        string Sex = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString();
        string JoinTime = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString();
        string Question = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString();
        string Answer = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text.ToString();
        string profession = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[8].Controls[0])).Text.ToString();
        SqlDataReader dr;
        string sql = "Update Student Set Name='" + Name + "',PWD='" + PWD + "',Sex='" + Sex + "',JoinTime='" + JoinTime + "',Question='" + Question + "',Answer='" + Answer + "',profession='" + profession + "'  Where ID=" + ID + "";
        SqlCommand myCommand = new SqlCommand(sql, cn);
        dr = myCommand.ExecuteReader();
        GridView1.EditIndex = -1;
        cn.Close();
        if (TextBox1.Text == "")
        {
            dataconn.bindinfostring(GridView1, "SELECT a.[ID],a.bumenid,a.danweiid, a.[Name], a.[kaoshenghao], b.danweimingchen, c.bumenmingcheng FROM [Student] a,danwei b,bumeng c where a.danweiid=b.id and a.bumenid=c.id order by a.[Name]", "ID");
        }
        else
        {
            dataconn.bind(GridView1, "SELECT a.[ID],a.bumenid,a.danweiid, a.[Name], a.[kaoshenghao], b.danweimingchen, c.bumenmingcheng FROM [Student] a,danwei b,bumeng c where a.danweiid=b.id and a.bumenid=c.id and a.name='" + TextBox1.Text + "' order by a.[Name]");
        }
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        if (TextBox1.Text == "")
        {
            dataconn.bindinfostring(GridView1, "SELECT a.[ID],a.bumenid,a.danweiid, a.[Name], a.[kaoshenghao], b.danweimingchen, c.bumenmingcheng FROM [Student] a,danwei b,bumeng c where a.danweiid=b.id and a.bumenid=c.id order by a.[Name]", "ID");
        }
        else
        {
            dataconn.bind(GridView1, "SELECT a.[ID],a.bumenid,a.danweiid, a.[Name], a.[kaoshenghao], b.danweimingchen, c.bumenmingcheng FROM [Student] a,danwei b,bumeng c where a.danweiid=b.id and a.bumenid=c.id and a.name='" + TextBox1.Text + "' order by a.[Name]");
        }
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        HiddenField1.Value = GridView1.SelectedDataKey.Value.ToString();
        if (HiddenField1.Value != "")
        {
            dataconn.ecDropDownList(DropDownList1, "select * from bumeng  where  danweiid =" + HiddenField1.Value , "bumenmingcheng", "id");  
            ;
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (HiddenField1.Value == "")
        {
            return;
        }
        
        
        if (TextBox2.Text == "")
        {
            return;
        }
        SqlConnection cn = dataconn.getcon();
        cn.Open();
        SqlDataReader dr;
        string sql = "insert into Student (Name,bumenid,danweiid) values ('" + TextBox2.Text + "'," + DropDownList1.SelectedValue.ToString() + "," + HiddenField1.Value + ")";
        SqlCommand myCommand = new SqlCommand(sql, cn);
        dr = myCommand.ExecuteReader();
        //GridView1.EditIndex = -1;
        cn.Close();
        if (TextBox2.Text == "")
        {
            //dataconn.bindinfostring(GridView1, "SELECT a.[ID] as id,a.bumenid,a.danweiid, a.[Name], a.[kaoshenghao], b.danweimingchen, c.bumenmingcheng FROM [Student] a,danwei b,bumeng c where a.danweiid=b.id and a.bumenid=c.id order by a.[Name]", "ID");
        }
        else
        {
            dataconn.bind(GridView2, "SELECT a.[ID] as id,a.bumenid,a.danweiid, a.[Name], a.[kaoshenghao], b.danweimingchen, c.bumenmingcheng FROM [Student] a,danwei b,bumeng c where a.danweiid=b.id and a.bumenid=c.id and a.name='" + TextBox2.Text + "' order by a.[Name]");
        }

    }
}